package main

import (
	"fmt"
	"math"
	"path/filepath"

	"demserver/src/elevation"
)

func main() {
	// Use the actual GeoTIFF file
	filename := "./demdata/scdjy.tif"

	// Get absolute path to the file
	absPath, err := filepath.Abs(filename)
	if err != nil {
		fmt.Printf("Failed to get absolute path: %v\n", err)
		return
	}

	fmt.Printf("Loading GeoTIFF file: %s\n", absPath)
	geoTiff, err := elevation.LoadGeoTIFF(absPath)
	if err != nil {
		fmt.Printf("Failed to load GeoTIFF: %v\n", err)
		return
	}
	fmt.Println("GeoTIFF loaded successfully")

	// Test with the provided high precision coordinates
	lat := 31.03021050140226
	lon := 103.55147461467699
	expected := 837.6951116196124

	x, y, err := geoTiff.LatLonToPixel(lat, lon)
	if err != nil {
		fmt.Printf("Failed to convert coordinates (%.14f, %.14f): %v\n", lat, lon, err)
		return
	}

	// Get elevation value with high precision scale factor
	elevationVal, err := geoTiff.GetElevation(x, y)
	if err != nil {
		fmt.Printf("Failed to get elevation at (%.14f, %.14f): %v\n", x, y, err)
		return
	}

	fmt.Printf("Input coordinates:\n")
	fmt.Printf("  Lat: %.14f\n", lat)
	fmt.Printf("  Lon: %.14f\n", lon)
	fmt.Printf("Pixel coordinates:\n")
	fmt.Printf("  X: %.14f\n", x)
	fmt.Printf("  Y: %.14f\n", y)

	fmt.Printf("\nElevation results:\n")
	fmt.Printf("  Expected elevation: %.14f\n", expected)
	fmt.Printf("  Actual elevation: %.14f\n", elevationVal)
	fmt.Printf("  Difference: %.14f\n", elevationVal-expected)

	// Check if the difference is within an acceptable range (e.g., 0.001)
	if math.Abs(elevationVal-expected) < 0.001 {
		fmt.Printf("\nResult: SUCCESS - Difference is within acceptable range\n")
	} else {
		fmt.Printf("\nResult: FAILED - Difference exceeds acceptable range\n")
	}
}
